<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/icon.css">  

<script src="<%=request.getContextPath() %>/js/jquery-1.11.2.min.js"></script> 
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.easyui.min.js"></script> 

</head>
<body>
 

   <table class="easyui-datagrid"  
        data-options="url:'<%=request.getContextPath() %>/category/getCategoryList',singleSelect:true,fitColumns:true"  id="dg">   
    <thead>   
        <tr>   
            <th data-options="field:'cid',width:100">一级分类id</th>   
            <th data-options="field:'cname',width:100">一级分类名称</th>    
        </tr>   
    </thead>   
</table>  
  <!-- 添加+++++++++++++++++++++++++++++ -->
  <div id="dd" class="easyui-dialog" title="添加" style="width:400px;height:200px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#btn'">   
     <div id="dds">    
   	 <form id="addForm" action="">
   		<table>
   			<tr>
   				<td><input type="hidden" name="cid"></td>
   			</tr>
   			<tr>
   				<td>名称：</td>
   				<td><input type="text" name="cname"></td>
   			</tr>
   			<tr>
   				<td><input type="submit" name="提交"></td>
   			</tr>
   		</table>
   		
   	</form> 
   </div>     
</div> 


 
  


<div id="btn">
			<a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">添加</a>  
			<a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">取消</a>
			  
</div>






<!-- 把js放在下面目的在于先加载easyui标签        ---后加载js  ==否则页面错乱 -->
<script type="text/javascript">

$('#dg').datagrid({
	toolbar: [{
		iconCls: 'icon-add',
		handler: function(){
			 
			$("#dd").dialog('open');
			//实现添加的操作
					 $('#addForm').form({    
		   				 url:"<%=request.getContextPath() %>/category/addCategory",    
		  				  onSubmit: function(){    
		  					 //首先加载，返回验证
		  					  return $("#addForm").form('validate');    
				   		 },    
				    success:function(obj){    
				    	if(obj){
				    		//加强版弹框
							$.messager.alert("系统提示","保存成功");
							$("#dd").dialog("close");
							$("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示","保存失败");
						}    
				    }    
				});    
				// submit the form    
				
					
			
			
		}
	},'-',{
		iconCls: 'icon-remove',
		handler: function(){
			var row = $("#dg").datagrid("getSelected");
			
			//删除功能
			$.ajax({
				url:"<%=request.getContextPath() %>/category/removeCategoeyByCid",
				type:"post",
				data:{cid:row.cid},
				success:function(obj){
					alert("删除成功");
					$('#dg').datagrid('reload');
				},
				dataType:"text",
			})
			
		}
	},'-',{
		iconCls: 'icon-edit',
		handler: function(){
			var row = $("#dg").datagrid("getSelected");
			//首先进行判断是否为null
			if(row!=null){
				//数据的辉县
				$("#addForm").form('load',row);
				//打开dialog
				$("#dd").dialog('open');
				//转换表单提交为ajax的提交
				$('#addForm').form({    
	   				 url:"<%=request.getContextPath() %>/category/updateCategoryByCid",    
	  				  onSubmit: function(){    
	  					 //首先加载，返回验证
	  					  return $("#addForm").form('validate');    
			   		 },    
			    success:function(obj){    
			    	if(obj){
			    		//加强版弹框
						$.messager.alert("系统提示","修改成功");
						//关闭dialog
			    		$("#dd").dialog("close");
						//刷新datagrid
						$("#dg").datagrid("reload");
					}else{
						$.messager.alert("系统提示","修改失败");
					}    
			    }    
			});   
				
			}else{
				$.messager.alert("系统提示","请选择一行");
			}

			
			
		}
	}
	
	
	
	
	]
});



</script> 
	
	
</body>
</html>